package com.res.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.res.entity.Register;
import com.res.entity.Student;
import com.res.entity.Teacher;
import com.res.service.impl.RegisterServiceImpl;
import com.res.util.FenyeUtil;

@RestController
@RequestMapping("/register")
public class RegisterController {

	@Autowired
	private RegisterServiceImpl registerService;
	@GetMapping
	public Object getAll(String stuname,String tname,Integer sid,Integer did,String maxTime,String minTime,Integer page,Integer limit) {
		//创建分页类
		FenyeUtil<Register> f=new FenyeUtil<Register>();
		//创建pageable
		Pageable pageable=PageRequest.of(page-1, limit);
		//存储data和count
		f.setData(registerService.getAllRegister(pageable, stuname, tname, sid, did, maxTime, minTime).getContent());
		f.setCount((int) registerService.getAllRegister(pageable, stuname, tname, sid, did, maxTime, minTime).getTotalElements());
		return f;
	}
	
	@DeleteMapping
	public Object deleteR(Integer rid) {
		return registerService.deleteR(rid);
	}
	
	@GetMapping("/getRegister")
	public Object getRegisterById(Integer stuid,Integer tid,Integer type) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//type0 表示登录的是学生 ，1 是老师 
		
		if(type==0) {
			//判断查询值是否为空 存在返回1  反之0
			if(registerService.getRegisterById(stuid, 1)!=null) {
				return 1;
			}//0可以进 1已在使用 
			//创建日志记录对象 存放值
			Register r=new Register();
			Student stu=new Student();//生成学生对象 放stuid
			stu.setStuid(stuid);
			Teacher teacher=new Teacher();
			teacher.setTid(1);//将老师的id设置为复制数据的id
			r.setStu(stu);
			r.setTeacher(teacher);//将老师 学生 加入到记录中
			//定义进入时间
			
			r.setIntime(sdf.format(new Date()));
			registerService.addR(r);
			return 0;
		    }else {
			if(registerService.getRegisterById(1, tid)!=null) {
				return 1;
			}
			//创建日志记录对象 存放值
			Register r=new Register();
			Student stu=new Student();//将学生的id设置为复制数据的id
			stu.setStuid(1);
			Teacher teacher=new Teacher();
			teacher.setTid(tid);
			r.setStu(stu);
			r.setIntime(sdf.format(new Date()));
			r.setTeacher(teacher);//将老师 学生 加入到记录中
			registerService.addR(r);
			return 0;
		}
		
	}
}
